package com.fsy.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fsy.entities.Book;
import com.fsy.service.BookService;
import com.fsy.utils.Result;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.util.StringUtils;

import javax.annotation.Resource;
import javax.annotation.Resources;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/book")
public class BookController {
    //注入service
    @Resource
    private BookService bookService;


    /**
     * 查询
     * @param book
     * @return
     */
    @GetMapping("/findByBook")
    public Map<String,Object> findByBook(Book book){
        //查询全部用户信息
        List<Book> bookList = bookService.findByBook(book);
        //查询全部用户数量
        int count = bookService.selectBookCounts(book);
        return Result.toClient("0", "",count,bookList);
    }

    //新增
    @PostMapping
    public Map<String,Object> add(@ModelAttribute Book book){
        boolean save = bookService.save(book);
        Map<String,Object> map = new HashMap<>();
        if (save){
            map.put("code","0");
            map.put("msg","新增成功");
        }else {
            map.put("code","1");
            map.put("msg","新增失败");
        }
        return map;
    }

    //修改
    @PutMapping
    public Map<String,Object> update(@ModelAttribute Book book){
        boolean update = bookService.updateById(book);
        Map<String,Object> map = new HashMap<>();
        if (update){
            map.put("code","0");
            map.put("msg","修改成功");
        }else {
            map.put("code","1");
            map.put("msg","修改失败");
        }
        return map;
    }

    //删除
    @DeleteMapping("/{id}")
    public Map<String,Object> del(@PathVariable("id") Integer id){
        boolean del = bookService.removeById(id);
        Map<String,Object> map = new HashMap<>();
        if (del){
            map.put("code","0");
            map.put("msg","删除成功");
        }else {
            map.put("code","1");
            map.put("msg","删除失败");
        }
        return map;
    }

    //删除
    @DeleteMapping("/BookDel")
    public Map<String,Object> batchDel(List<Integer> list){
        boolean del = bookService.removeBatchByIds(list);
        Map<String,Object> map = new HashMap<>();
        if (del){
            map.put("code","0");
            map.put("msg","删除成功");
        }else {
            map.put("code","1");
            map.put("msg","删除失败");
        }
        return map;
    }
}
